from django.shortcuts import render
from django.http.response import HttpResponse,JsonResponse
from django.views import View
from . import forms,models,tools,const
# Create your views here.
import time,json
import django_redis
import pickle


def my_view(request):
    return JsonResponse({"code":0})


class MyView(View):
    def get(self,request):
        return JsonResponse({"code":"get"})

    def post(self,request):
        return JsonResponse({"code":"post"})

    def delete(self,request):
        return JsonResponse({"code":"delete"})

    def put(self,request):
        return JsonResponse({"code":"put"})


class Register(View):

    def get(self,request):
        form = forms.RegisterForm(request.GET)
        if form.is_valid():
            #{"phone":"xxx","email":"xxxx","password":"xxxxxx","cpassword":"2345235"} form.cleaned_data
            # models.User.objects.create(**form.cleaned_data)
            models.User.objects.create(**form.cleaned_data)
            ret = {"code":0,"msg":"注册成功"}
        else:
            ret = {"code":-1,"msg":form.errors.get_json_data()}

        return JsonResponse(ret,json_dumps_params={"ensure_ascii":False})

class Register2(View):

    def get(self,request):
        form = forms.RegisterForm2(request.GET)
        if form.is_valid():
            #{"phone":"xxx","email":"xxxx","password":"xxxxxx","cpassword":"2345235"} form.cleaned_data
            # models.User.objects.create(**form.cleaned_data)
            models.User.objects.create(**form.cleaned_data)
            ret = {"code":0,"msg":"注册成功"}
        else:
            ret = {"code":-1,"msg":form.errors.get_json_data()}

        return JsonResponse(ret,json_dumps_params={"ensure_ascii":False})
#http://127.0.0.1:8000/register?phone=18612532945&email=niuhanyang@qq.com&password=123456&cpassword=123456

# 第一种方式
# class Login(View):
#     def get(self,request):
#         form = forms.LoginForm(request.GET)
#         if form.is_valid():
#             user = form.user
#             r = django_redis.get_redis_connection("dev")#用django_redis连接redis
#             # r=tools.get_redis()#用自己写的函数连接redis
#             sessionid = r.get(const.login_key_prefix+user.phone) #是否存在
#             if not sessionid:
#                 sessionid = tools.md5("%s%s" % (user.phone,time.time()))
#                 r.set(const.login_key_prefix+user.phone,sessionid,const.login_expire)
#                 data = {"user_id":user.id,"phone":user.phone,"email":user.email}
#                 r.set(const.login_key_prefix + sessionid,json.dumps(data),const.login_expire)
#             data = {"code":0,"msg":"登录成功","token":sessionid}
#         else:
#             data = {"code":-1,"msg":form.errors.get_json_data()}
#         return JsonResponse(data)


#用pickle方式
class Login(View):
    def get(self,request):
        form = forms.LoginForm(request.GET)
        if form.is_valid():
            user = form.user
            r = django_redis.get_redis_connection("dev")#用django_redis连接redis
            # r=tools.get_redis()#用自己写的函数连接redis
            sessionid = r.get(const.login_key_prefix+user.phone) #是否存在
            if not sessionid:
                sessionid = tools.md5("%s%s" % (user.phone,time.time()))
                r.set(const.login_key_prefix+user.phone,sessionid,const.login_expire)
                # data = {"user_id":user.id,"phone":user.phone,"email":user.email}
                user_obj=pickle.dumps(user)
                r.set(const.login_key_prefix + sessionid,json.dumps(user_obj),const.login_expire)
            data = {"code":0,"msg":"登录成功","token":sessionid}
        else:
            data = {"code":-1,"msg":form.errors.get_json_data()}
        return JsonResponse(data)
    #dfc1c03e086ea0065a037ac7a06f9bec
    #wsc : dfc1c03e086ea0065a037ac7a06f9bec

class Pay(View):
    def get(self,request):
        form = forms.PayForm(request.GET)
        if form.is_valid():
            money = form.cleaned_data.get("money")
            form.user_obj.money -= money #扣钱
            form.user_obj.save()
            ret = {"code":0,"msg":"支付成功"}
        else:
            ret = {"code":-1,"msg":form.errors.get_json_data()}

        return JsonResponse(ret,json_dumps_params={"ensure_ascii":False})